package com.leectrl;

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;

import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.leectrl.server.AITCPServer;
import com.leectrl.server.AndroidSocketServer;
import com.leectrl.server.DockingTCPServer;
import com.leectrl.server.HttpServer;
import com.leectrl.server.SocketServer;
import com.leectrl.server.UdpServer;
import com.leectrl.utils.StorageManager;

public class IOTServer extends Service {
    @Override
    public void onCreate() {
        super.onCreate();
        startZigbeeTcpServer();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    new DockingTCPServer().run();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                new SocketServer().run();
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    new UdpServer().run();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
        SharedPreferences sharedPreferences=getSharedPreferences("school_iot", 0);
        String webEnableStr = sharedPreferences.getString("c_setting_webEnable", null);
        if (webEnableStr != null) {
            JSONObject webEnableConfig = JSON.parseObject(webEnableStr);
            if (webEnableConfig != null && webEnableConfig.containsKey("enable") && webEnableConfig.getBoolean("enable")) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            try {
                                new HttpServer().run();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        }else{
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    try {
//                        try {
//                            new HttpServer().run();
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                        }
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                }
//            }).start();
        }
        String aiConfigStr = sharedPreferences.getString("c_setting_aiConfig", null);
        if (aiConfigStr != null) {
            JSONObject aiConfig = JSON.parseObject(aiConfigStr);
            if (aiConfig != null && aiConfig.containsKey("enable") && aiConfig.getBoolean("enable")) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            try {
                                  new AITCPServer().run();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        }

    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        // write your code here
        return null;
    }


    private void startZigbeeTcpServer() {
        StorageManager.getInstance().setCacheItem("gatewayOnline", "0");
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    new AndroidSocketServer().run();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}